<!--
- Author:   .
- Date:    2017/7/11 0011.
- File:    list.
-->
<template>
  <div>
    <div class="Web_Box">
      <div class="gd lt0 wauto z99">
        <div class="Tabs2">
          <ul>
            <li :class="{'on': type == 0}" style="width: 20%" @click="switchType(0)">全部</li>
            <li :class="{'on': type == 1}" style="width: 20%" @click="switchType(1)">待付款</li>
            <li :class="{'on': type == 2}" style="width: 20%" @click="switchType(2)">待发货</li>
            <li :class="{'on': type == 3}" style="width: 20%" @click="switchType(3)">待收货</li>
            <li :class="{'on': type == 4}" style="width: 20%" @click="switchType(4)">已完成</li>
          </ul>
        </div>
      </div>
      <!--<div class="ConsigneeInfo">-->
        <!--<div class="add_adree" v-show="addressList.data == ''" @click="gotoAddAddress">添加收货地址</div>-->
        <!--<div @click="selectAddress" v-show="addressList.data != ''">-->
          <!--<p class="text1 f-cb"><span class="name fl">收货人:{{address.true_name}} </span>-->
            <!--<span class="tel fr">{{address.mob_phone}}</span></p>-->
          <!--<div class="Address slh2">{{address.area_info}} <span>{{address.address}}</span>-->
            <!--<i class="iconfont icon-rightdot"></i></div>-->
          <!--</div>-->

      <!--</div>-->
      <div class="add_adree" @click="gotoAddAddress(1,0)" v-if="signData == 1"><span>批量添加收货地址</span></div>
      <div class="DistributionOrder">
        <div class="ProList pt10">
          <div class="SearchBox p20 bg_efeff4" v-show="false">
            <div class="SearchBor xd hidden"><i class="iconfont icon-search lh70"></i>
              <input type="text" class="SearchInp pl80 f26" placeholder="搜索您需要的订单" v-model="keyword">
              <button type="button" class="SearchBut" @click="searchOrder">搜索</button>
            </div>
          </div>
          <ul>
            <!--v-infinite-scroll="loadMoreOrder"-->
            <!--infinite-scroll-disabled="false"-->
            <!--infinite-scroll-distance="0"-->
            <!--infinite-scroll-immediate-check="false">-->
            <li v-for="order in orderList.data">
            <!--<li v-for="order in orderList.data" v-show="order.order_info.order_state != $dictionary.orderStatus.unpay">-->
            <div class="tit"><span class="fl">订单号：{{order.order_info.order_sn}}</span>
                <span v-if="order.order_info.refund_state == '2'" class="fr cr_595757">退款订单</span>
                <span v-if="order.order_info.refund_state == '0'" class="fr cr_595757">{{order.order_info.state_desc}}</span>
              </div>
              <div style="margin-bottom: 10px;" class="giftbox f-cb" v-for="goods in order.order_info.extend_order_goods" @click="gotoOrderDetail(order.order_info.order_id)">
                <div class="Pic">
                  <img :src="hostName + $config.goodsImgSrc + goods.goods_image" alt=""/>
                </div>
                <div class="Con" style="width:11.5rem;">
                  <div class="">
                    <h2 class="">{{goods.goods_name}}&nbsp;&nbsp;<span v-for="attr in goods.goods_spec">{{attr.name}} </span>
                    </h2>
                  </div>
                  <div class="">
                    <span class="Prices" v-show="goods.gc_id != '1038'">￥{{goods.goods_price}}</span>
                    <span class="Prices" v-show="goods.gc_id == '1038'"><i class="iconfont icon-zengpin1"></i></span>
                    <span class="Num fr">×{{goods.goods_num}}</span>
                  </div>
                </div>
              </div>
              <div class="Operation">
                <div class="Deductible" v-show="memberInfo.is_brush_man == '1' && order.order_info.is_needship == '0'">获得积分:{{parseFloat(order.order_info.get_freeze_balance || 0) + parseFloat(order.order_info.order_amount)}}</div>
                <p class="Total tr">共{{countOrderGoodsNum(order)}}件商品&nbsp;&nbsp;合计:￥{{order.order_info.order_amount || 0}}</p>
                <!--<span class=" pt10 disib f22 fl" v-show="order.order_info.is_needship == '0'">无需寄货</span>-->
                <!--<span class=" pt10 disib f22 fl" v-show="order.order_info.is_needship == '1'">需要寄货</span>-->
                <div class="fr">
                  <span class="but1 cr_595757 fr ml20" v-show="(memberInfo.member_id == '11118896' || memberInfo.member_id == '11118895') && !order.order_info.if_lock && order.order_info.order_state == $dictionary.orderStatus.payed" @click="addRefundMoney(order.order_info.order_id, order.order_info.extend_order_goods[0].goods_id)">申请退款 </span>
                  <span class="but1 cr_45a73a fr ml20" v-show="order.order_info.order_state == $dictionary.orderStatus.unpay" @click="pay(order)">立即付款 </span>
                  <!--<span class="but1 cr_595757 fr ml20" v-show="order.order_info.order_state == $dictionary.orderStatus.unpay" @click="cancleOrder(order.order_info.order_id)">取消订单 </span>-->
                  <!--<span class="but1 cr_45a73a fr ml20" v-show="order.order_info.order_state == $dictionary.orderStatus.sended" @click="confirmReceived(order.order_info.order_id)">确认收货 </span>-->
                  <!--<span class="but1 cr_595757 fr ml20" v-show="order.order_info.order_state == $dictionary.orderStatus.sended" @click="gotoOrderExpress(order.order_info.order_id)">查看物流 </span>-->
                  <!--<span class="but1 fr cr_45a73a ml20" v-show="!order.order_info.if_lock && order.order_info.order_state == $dictionary.orderStatus.recevied && order.order_info.evaluation_state == 0" @click="addComment(order.order_info.order_id)">立即评价 </span>-->
                </div>
              </div>
              <div style="margin-top: 28px" class="add_adree" v-show="order.order_info.address_add == 1" @click="gotoAddAddress(0, order.order_info.order_id)">添加收货地址
              </div>
              <div style="float: right; margin-bottom: 5px; margin-right: 15px" v-show="order.order_info.address_add == 1">小提示：快递运费到达支付</div>
            </li>
          </ul>
          <loading-more :allLoaded="orderList.allLoaded" :show="orderList.data.length > 0"></loading-more>
        </div>
      </div>
    </div>
    <empty-data :show="orderList.allLoaded && orderList.data.length <= 0"></empty-data>
    <!--<footer-tab></footer-tab>-->
    <div class="pl20 pr20 mt35" style="position: fixed; bottom:  10px; width: 100%">
      <button type="button" class="but_1 wauto" @click="back">返回</button>
    </div>
  </div>
</template>

<script>
  import {MessageBox} from 'mint-ui'
  import payPop from '../../components/payPop'
  import {mapModules, mapRules} from 'vuet'

  export default {
    mixins: [
      mapModules({addressList: 'addressList', memberInfo: 'memberInfo'}),
      mapRules({need: ['addressList', 'memberInfo']})
    ],
    data() {
      return {
        type: this.$route.query.type || 0, // 默认0，全部类型
        keyword: '', // 搜索关键字
        orderList: {requested: false, loading: false, allLoaded: false, page: 1, pageSize: 500, data: []},
        signData: 0,
        hostName: 'http://' + window.location.hostname
      }
    },
    components: {},
    created() {
      this.getOrderList(this.type, this.orderList.page, this.orderList.pageSize)
      // for (let i = 0; i <  )
      // this.$toast(this.orderList.data[0])
    },
    watch: {
      $route() {
        this.type = this.$route.query.type
        this.initOrderList()
        this.getOrderList(this.type, this.orderList.page, this.orderList.pageSize)
      }
    },
    computed: {
      payedMoney: function () {
        if (this.memberInfo.sub_fee == '0.00') {
          return parseInt(this.memberInfo.handle_fee) + parseInt(this.memberInfo.bj_sub_fee)
        } else {
          return parseInt(this.memberInfo.sub_fee)
        }
      }
      // sign: function () {
      //   for (let i = 0; i < 4; i++) {
      //     if (this.orderList.data[0].order_info.address_add == 1) {
      //       return 0
      //     } else {
      //       return 1
      //     }
      //   }
      // }
    },
    methods: {
      gotoAddAddress(pl, order_id) {
        // this.$toast(order_id)
        this.$router.push({
          name: 'address-orderAddress',
          query: {order_id: order_id, pl: pl, from: 'order-list'}
        })
      },
      back() {
        this.$router.push({name: 'my'})
      },
      countOrderGoodsNum(order) {
        let count = 0
        if (!order.order_info || !order.order_info.extend_order_goods) {
          return count
        }
        for (let i = 0; i < order.order_info.extend_order_goods.length; i++) {
          count += parseInt(order.order_info.extend_order_goods[i].goods_num)
        }
        return count
      },
      gotoOrderExpress(id) {
        this.$router.push({name: 'order-express', params: {id: id}})
      },
      gotoOrderDetail(id) {
        this.$router.push({name: 'order-detail', params: {id: id}})
      },
      switchType(value) {
        this.$router.replace({name: 'order-list', query: {type: value}})
      },
      initOrderList() {
        this.orderList = {requested: false, loading: false, allLoaded: false, page: 1, pageSize: 10, data: []}
      },
      getOrderList(type, page, pageSize) {
        this.orderList.loading = true
        this.$request.getOrderList(type, page, pageSize).then(data => {
          this.orderList.allLoaded = true
          // if (data.length < this.orderList.pageSize) {
          //   this.orderList.allLoaded = true
          // }
          this.orderList.data = [...this.orderList.data, ...data]

          for (let i = 0; i < this.orderList.data.length; i++) {
            if (this.orderList.data[i].order_info.address_add == 1) {
              this.signData = 1
              return
            }
          }
        }).finally(() => {
          this.orderList.requested = true
          this.orderList.loading = false
        })
      },
      loadMoreOrder() {
        if (this.orderList.loading || this.orderList.allLoaded) { // 正在加载 或 已取得全部数据
          return
        }
        setTimeout(() => {
          this.getOrderList(this.type, ++this.orderList.page, this.orderList.pageSize)
        }, 500)
      },
      cancleOrder(id) {
        MessageBox.confirm(`你确定要取消订单吗?`).then(action => {
          this.$request.cancleOrder(id).then(data => {
            this.$toast('取消订单成功')
            this.initOrderList()
            this.getOrderList(this.type, this.orderList.page, this.orderList.pageSize)
          }).catch(e => {
            this.$toast('取消订单失败：' + e.msg)
          })
        }).catch(() => {
        })
      },
      searchOrder() {
      },
      addComment(id) {
        this.$router.push({name: 'comment-add', params: {id: id}})
      },
      addRefundGoods(orderId, goodsId) {
        this.$router.push({name: 'refund-goods-add', params: {orderId: orderId, goodsId: goodsId}})
      },
      addRefundMoney(orderId, goodsId) {
        this.$router.push({name: 'refund-money-add', params: {orderId: orderId, goodsId: goodsId}})
      },
      confirmReceived(id) {
        this.$request.confirmReceived(id).then(data => {
          this.$toast('确认收货成功')
          for (let i = 0; i < this.orderList.data.length; i++) {
            if (this.orderList.data[i].order_info.order_id == id) {
              this.orderList.data.splice(i, 1)
              break
            }
          }
        }).catch(e => {
          this.$toast('确认收货失败：' + e.msg)
        })
      },
      pay(order) {
        if (order.order_info.extend_order_goods[0].goods_id == this.$config.goodsId1 && (this.memberInfo.handle_fee != '0.00' || this.memberInfo.sub_fee != '0.00')) {
          this.$toast('您已付过款了')
          return
        }
        if (order.order_info.extend_order_goods[0].goods_id == this.$config.goodsId2 && (this.memberInfo.bj_sub_fee != '0.00' || this.memberInfo.sub_fee != '0.00')) {
          this.$toast('您已付过款了')
          return
        }
        if (order.order_info.extend_order_goods[0].goods_id == this.$config.goodsId3 && (this.memberInfo.handle_fee != '0.00' || this.memberInfo.bj_sub_fee != '0.00' || this.memberInfo.sub_fee != '0.00')) {
          this.$toast('您已付过款了')
          return
        }
        let paySn = order.order_info.pay_sn
        payPop({paySn: paySn, orderId: order.order_info.order_id})
      }
    }
  }
</script>

<style>
  .but1 {
    display: inline-block;
    height: 1.3rem;
    line-height: 1.25rem;
    text-align: center;
    font-size: 0.6rem;
    border: #dcdddd solid 1px;
    border-radius: 0.125rem;
    padding: 0 0.375rem;
  }
</style>
